package com.autotest.glin.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnection {

    public static Logger log = Logger.getLogger(DBConnection.class);
    private static DruidDataSource druidDataSource = null;

    static {
        try {
            Properties props = new Properties();
            props.load(new FileInputStream("src/main/resources/db.properties"));
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DruidPooledConnection getConnection() throws SQLException {
        DruidPooledConnection connection = null;
        connection = druidDataSource.getConnection();
        if (connection == null) {
            System.exit(0);
        }
        return connection;
    }

    public static void main(String[] args) throws SQLException {
        System.out.println(getConnection());
    }
}
